Given an array of positive integers nums
, return the number of distinct prime factors in the product of the elements ofnums
.
Note that:
- A number greater than
1
is called prime if it is divisible by only1
and itself. - An integer
val1
is a factor of another integerval2
ifval2 / val1
is an integer.
Input: nums = [2,4,3,7,10,6] Output: 4 Explanation: The product of all the elements in nums is: 2 * 4 * 3 * 7 * 10 * 6 = 10080 = 25 * 32 * 5 * 7. There are 4 distinct prime factors so we return 4.
Input: nums = [2,4,8,16] Output: 1 Explanation: The product of all the elements in nums is: 2 * 4 * 8 * 16 = 1024 = 210. There is 1 distinct prime factor so we return 1.
1 <= nums.length <= 104
2 <= nums[i] <= 1000
implSolution{pubfndistinct_prime_factors(nums:Vec<i32>) -> i32{let max_num = *nums.iter().max().unwrap();letmut primes = vec![];for i in2..=max_num {if(2..=(i asf64).sqrt()asi32).all(|j| i % j != 0){ primes.push(i);}}for i in0..nums.len(){for j in0..primes.len(){if nums[i] < primes[j]{break;}if nums[i] % primes[j] == 0{ primes[j] = 1;}}} primes.iter().filter(|&&x| x == 1).count()asi32}}